import { Entity, Column, ManyToOne, JoinColumn } from "typeorm";

import type { INTEGER_PG, VARCHAR255_PG, TonSku, TonOrder, TonOrderGroup } from "qqlx-core";
import { ENUM_TON_ORDER, TonSkuGateway } from "qqlx-core";

import { TransformerInteger } from "@lib/orm.transfor.number";
import { TransformerVarchar50, TransformerVarchar255 } from "@lib/orm.transfor.string";
import { TransformerEnum } from "@lib/orm.transfor.enum";
import { TransformerBoolean } from "@lib/orm.transfor.boolean";

import { BigIdSkuSchema } from "@/alpha/ext.com.sku-id-big";

@Entity({ name: TonSkuGateway.relations })
export class TonSkuSchema extends BigIdSkuSchema implements TonSku {
  @Column({ transformer: new TransformerVarchar255() })
  ivid: VARCHAR255_PG = "";

  @Column({ transformer: new TransformerVarchar255() })
  labelStr: VARCHAR255_PG = "";

  @Column({ transformer: new TransformerVarchar255() })
  formulaName: VARCHAR255_PG = "";

  @Column({ transformer: new TransformerVarchar255() })
  formulaContent: VARCHAR255_PG = "";

  @Column({ transformer: new TransformerEnum(Object.values(ENUM_TON_ORDER), ENUM_TON_ORDER.NONE) })
  type: ENUM_TON_ORDER = ENUM_TON_ORDER.NONE;

  @Column({ transformer: new TransformerInteger(1000) })
  ton: INTEGER_PG = 0;

  @Column({ transformer: new TransformerBoolean() })
  isTonAble: boolean = false;

  @Column({ transformer: new TransformerBoolean() })
  isOrderDisabled: boolean = false;

  // ================================

  @Column({ transformer: new TransformerInteger() })
  refTonOrderGroupId!: TonOrderGroup["id"];

  @ManyToOne("TonOrderGroupSchema", (s: TonOrderGroup) => s.joinTonSkuList)
  @JoinColumn({ name: "refTonOrderGroupId", referencedColumnName: "id" })
  justTonOrderGroup!: TonOrderGroup;

  @Column({ transformer: new TransformerVarchar50() })
  refTonOrderId!: TonOrder["uuid32"];

  @ManyToOne("TonOrderSchema", (s: TonOrder) => s.joinTonSkuList)
  @JoinColumn({ name: "refTonOrderId", referencedColumnName: "uuid32" })
  justTonOrder!: TonOrder;
}
